package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.*;

@Service
@Transactional
public class CarPackageAuditServiceImpl implements ICarPackageAuditService {

    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private TaskService taskService;

    @Override
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<CarPackageAudit>(carPackageAuditMapper.selectForList(qo));
    }

    @Override
    public InputStream processImg(Long id) {
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectByPrimaryKey(id);
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(carPackageAudit.getBpmnInfoId());
        List<String> highLightedActivities = new ArrayList<>();
        if (carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS)) {
            highLightedActivities = runtimeService.getActiveActivityIds(carPackageAudit.getInstanceId());
        }else {
            highLightedActivities = Collections.emptyList();
        }
        DefaultProcessDiagramGenerator defaultProcessDiagramGenerator = new DefaultProcessDiagramGenerator();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(bpmnInfo.getActProcessId());
        InputStream inputStream = defaultProcessDiagramGenerator.generateDiagram(bpmnModel, highLightedActivities, Collections.emptyList(), "宋体", "宋体", "宋体");
        return inputStream;

    }

    @Override
    @Transactional
    public void cancelApply(Long id) {
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectByPrimaryKey(id);
        if (!carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS)) {
            throw new BusinessException("非法操作");
        }
        carPackageAuditMapper.changeStatus(id,CarPackageAudit.STATUS_CANCEL);
        serviceItemMapper.changeAuditStatus(carPackageAudit.getServiceItemId(),ServiceItem.AUDITSTATUS_INIT);
        runtimeService.deleteProcessInstance(carPackageAudit.getInstanceId(),"不想要了");
    }

    @Override
    public void audit(Long id, Integer auditStatus, String info) {
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectByPrimaryKey(id);
        if (!carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS)) {
            throw new BusinessException("非法操作");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("auditStatus",auditStatus);
        Task currentTask = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
        taskService.complete(currentTask.getId(),map);
        if(auditStatus.equals(CarPackageAudit.STATUS_PASS)){
            Task nextTask = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
            if (nextTask != null){
                carPackageAudit.setAuditorId(Long.parseLong(nextTask.getAssignee()));
            }else {
                carPackageAudit.setStatus(CarPackageAudit.STATUS_PASS);
                serviceItemMapper.changeAuditStatus(carPackageAudit.getServiceItemId(),ServiceItem.AUDITSTATUS_APPROVED);
            }
            carPackageAudit.setInfo(carPackageAudit.getInfo() + "<br>" + ShiroUtils.getUser().getUserName() + "同意 审批意见为:" + info);
        }else if(auditStatus.equals(CarPackageAudit.STATUS_REJECT)){
            carPackageAudit.setStatus(CarPackageAudit.STATUS_REJECT);
            serviceItemMapper.changeAuditStatus(carPackageAudit.getServiceItemId(),ServiceItem.AUDITSTATUS_REPLY);
            carPackageAudit.setInfo(carPackageAudit.getInfo() + "<br>" + ShiroUtils.getUser().getUserName() + "拒绝 审批意见为:" + info);
        }
        carPackageAudit.setAuditTime(new Date());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);
    }
}
